<template>
    <div class="layout-container">
        <div class="layout-container-left">
            <UseTable :tableData="tableData" custom :labels="labels" @change="change" />
        </div>

        <div class="layout-container-right">
            <div class="form-box">
                <el-form label-width="auto">
                    <el-form-item label="基期整体总值">
                        <el-input v-model="paramsData.basePeriodTotal" type="number" />
                    </el-form-item>

                    <el-form-item label="报告期整体总值">
                        <el-input v-model="paramsData.reportPeriodTotal" type="number" />
                    </el-form-item>

                    <el-form-item>
                        <div class="data-select">
                            <DmData @dataChange="dataChange" />
                            <Upload @uploadSuccess="uploadSuccess" />
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleCalculate">计算</el-button>
                    </el-form-item>

                </el-form>
            </div>

            <el-divider />

            <div class="info-box">
                <h5>算法说明</h5>
                <p>计算各因素对整体指标变化的贡献值（贡献值因素变动其他因素均值）和贡献比例（贡献比例贡献值整体变动）。</p>

                <h5>使用范围</h5>
                <ul>
                    <li>评估因素对结果的影响（如销量、单价对利润增长的贡献）；</li>
                    <li>定位关键驱动因素（如 “销量增长贡献了 70% 的利润提升”）；</li>
                    <li>需明确基期和报告期的因素值及整体指标。</li>
                </ul>
            </div>
        </div>

        <Result v-model="resultShow" :result="resultData" />
    </div>
</template>

<script setup lang="ts">
import {onActivated, onMounted, ref} from "vue"
import {ElMessage}                   from "element-plus"
import Result                        from "./components/Result.vue"
import {getExcel}                    from "@/api/data"
import {contribution}                from "@/api/structure"
import Upload                        from "@/components/Upload.vue"
import UseTable                      from "@/components/UseTable.vue"
import DmData                        from "@/components/DmData.vue"

function dataChange(data) {
    tableData.value = data
}

function uploadSuccess(res) {
    tableData.value = res.data
}

let labels = ["影响名称", "基期", "报告期"]

function change(list) {
    paramsData.value.factorNames         = list?.find(item => item.label === "影响名称")?.value || []
    paramsData.value.basePeriodFactors   = list?.find(item => item.label === "基期")?.value || []
    paramsData.value.reportPeriodFactors = list?.find(item => item.label === "报告期")?.value || []
}

let paramsData = ref({
    factorNames        : [],
    basePeriodFactors  : [],
    reportPeriodFactors: [],
    basePeriodTotal    : 2000,
    reportPeriodTotal  : 3200
})

let resultShow = ref(false)
let resultData = ref("")

// 计算
function handleCalculate() {
    if (paramsData.value.factorNames.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }
    if (paramsData.value.basePeriodFactors.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }
    if (paramsData.value.reportPeriodFactors.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }

    contribution(paramsData.value).then((res: any) => {
        resultData.value = res?.data
        resultShow.value = true
    })
}

let loading   = ref(true)
let tableData = ref([])

let getTableData = () => {
    loading.value = true

    getExcel("contribution").then(res => res.data).then(data => tableData.value = data)

    loading.value = false
}

/**
 * 初始化参数
 */
function initParam() {

}

// -----------------------------主程序start-----------------------------------------
onMounted(() => {
    initParam()
    getTableData()
})

onActivated(() => {
    initParam()
    getTableData()
})
// -----------------------------主程序end-----------------------------------------

</script>

<style scoped>

</style>

